package com.init.engineering.dao;

import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pojo.Developer;
import com.init.engineering.pojo.Project;
import com.init.engineering.pojo.Stage;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface StageDao {

    /**
     * 查找所有工程阶段
     * 并查找该工程阶段的工程详情信息，实现一对一级联查询
     * @return
     */
    @Results(id = "stageMap", value = {
            @Result(column = "pid", property = "pid"),
            //EAGER:查询后立即加载，LAZY:只有当查询的数据被使用时，去查询加载
            @Result(column = "pid", property = "project",
                    javaType = Project.class,
                    one = @One(select = "com.init.engineering.dao.ProjectDao.findProjectByPid",
                            fetchType = FetchType.EAGER)),  //一对一关联查询
    })
    @Select("select * from stage where status = 1")
    List<Stage> findStages();

    /**
     * 分页查询工程阶段
     * 实现按条件查找的动态sql：
     * 若条件不为空，则带入条件进行查询，若条件为空，则查询所有工程
     * @param queryPageBean
     * @return
     */
    @ResultMap("stageMap")
    @Select({"<script> SELECT * FROM stage <where>" +
            "<if test=\"queryString!=null and queryString != ''\">" +
            "and sname like concat('%',#{queryString},'%')" +
            "</if>" +
            "and status=1" +
            "</where> limit #{currentPage},#{pageSize}" +
            "</script>"})
    List<Stage> findStagesByPage(QueryPageBean queryPageBean);


    /**
     * 查询符合分页查询的总条数
     * @param queryString
     * @return
     */
    @Select({"<script> SELECT count(*) FROM stage <where>" +
            "<if test=\"queryString!=null and queryString != ''\">" +
            "and sname like concat('%',#{queryString},'%')" +
            "</if>" +
            "and status=1" +
            "</where>" +
            "</script>"})
    long selectTotalCount(@Param("queryString")String queryString);


    /**
     * 通过工程阶段id查找
     * @param sid
     * @return
     */
    @Select("SELECT * FROM `stage` where sid = #{sid} and status = 1")
    Stage findStageBySid(String sid);

    /**
     * 逻辑删除工程阶段
     * 执行更新操作，将属性status的值修改为0，表示将该数据删除
     * @param sid
     * @return
     */
    @Update("UPDATE stage set status = 0 where sid = #{sid}")
    int updateStatusStage(@Param("sid")String sid);

    /**
     * 更新工程阶段信息
     * 但不能更新status属性
     * @param stage
     * @return
     */
    @Update("<script>update stage <set><if test='sname != null'>sname = #{sname},</if>" +
            "<if test='starttime != null'>starttime = #{starttime},</if>" +
            "<if test='endtime != null'>endtime = #{endtime},</if>" +
            "<if test='pid != null'>pid = #{pid},</if>" +
            "<if test='updatetime != null'>updatetime = #{updatetime}</if>" +
            "</set> where sid = #{sid}</script>")
    int updateStage(Stage stage);

    /**
     * 新建一个工程阶段
     * @param stage
     * @return
     */
    @Insert("insert into stage(sid,sname,starttime,endtime,pid,updatetime,status)" +
            " values(#{sid},#{sname},#{starttime},#{endtime},#{pid},#{updatetime},1)")
    int saveStage(Stage stage);

    @Select("select * from stage where pid = #{pid} and status = 1")
    List<Stage> findStageByPid(int pid);

}
